/************************************************************************
 ** This file is part of the network simulator Shawn.                  **
 ** Copyright (C) 2004,2005 by  SwarmNet (www.swarmnet.de)             **
 **                         and SWARMS   (www.swarms.de)               **
 ** Shawn is free software; you can redistribute it and/or modify it   **
 ** under the terms of the GNU General Public License, version 2.      **
 ************************************************************************/
#ifdef ENABLE_SHAWNBGU
#include "../buildfiles/_apps_enable_cmake.h"
#ifdef ENABLE_VIS
#include "legacyapps/shawnbgu/shawnbgu_vis_property_tag_shape_leader_int.h"
#include "apps/vis/elements/vis_drawable_node.h"
#include "sys/taggings/basic_tags.h"

using namespace shawn;

namespace shawnbgu
{

   PropertyTagShapeLeaderIntTask::PropertyTagShapeLeaderInt::
      PropertyTagShapeLeaderInt( const shawn::Node* n, std::string dynamictag )
      :  node_(n),
         tag_(dynamictag)
   {}
   // ----------------------------------------------------------------------
   PropertyTagShapeLeaderIntTask::PropertyTagShapeLeaderInt::
   ~PropertyTagShapeLeaderInt()
   {}
   // ----------------------------------------------------------------------
   int
   PropertyTagShapeLeaderIntTask::PropertyTagShapeLeaderInt::
   value( double,
          const PropertyStack<int>&,
          const Element& e) 
      const throw()
   { 
     //shawn::Vec out;
	   int out;
      //Looking for the provided (double) tag:
      bool tagval = read_tag(e);
      
      if(!tagval)
      {
         /*// Tag not available or not compatible, so the node gets a gray color:
         out = shawn::Vec(0.5, 0.5, 0.5);*/
    	  out = 1;
      }
      else
      {
           	  out = 2;
      }
      return out;
   }
   // ----------------------------------------------------------------------
   bool PropertyTagShapeLeaderIntTask::PropertyTagShapeLeaderInt::
      read_tag(const Element& e) const throw()
   {
      const DrawableNode* dn = dynamic_cast<const DrawableNode*>(&e);


      const shawn::BoolTag* btag = NULL;

      if(dn != NULL)
      {
         ConstTagHandle taghandle = dn->node().find_tag(tag_);
         if(taghandle != NULL)
         {
            btag = dynamic_cast<const BoolTag*>(taghandle.get());
         }
      }

      if(btag!=NULL)
         return btag->value();
      else
         return false;
   }
   // ----------------------------------------------------------------------
   PropertyTagShapeLeaderIntTask::
   PropertyTagShapeLeaderIntTask()
   {}
   // ----------------------------------------------------------------------
   PropertyTagShapeLeaderIntTask::
   ~PropertyTagShapeLeaderIntTask()
   {}
   // ----------------------------------------------------------------------
   std::string
   PropertyTagShapeLeaderIntTask::
   name( void )
      const throw()
   {
      return "vis_tag_shape_leader_int";
   }
   // ----------------------------------------------------------------------
   std::string
   PropertyTagShapeLeaderIntTask::
   description( void )
      const throw()
   {
      return "create a tag-dependend int";
   }
   // ----------------------------------------------------------------------
   ConstPropertyHandle
   PropertyTagShapeLeaderIntTask::
   create_property( shawn::SimulationController& sc )
      throw( std::runtime_error )
   {
      //if( !sc.environment().optional_string_param("node","").empty() )
      //   { ABORT_NOT_IMPLEMENTED; }
      
      std::string tagname = sc.environment().required_string_param("dynamictag");

      PropertyTagShapeLeaderInt* rtc = new PropertyTagShapeLeaderInt(NULL, tagname);
      try {
         fill_default_params(*rtc,sc);
      }
      catch( std::runtime_error& ) {
         delete rtc;
         throw;
      }
      return rtc;
   }


}
#endif
#endif
